package org.zhouhe.tag.article;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zhouhe.modules.article.service.ArticleService;
import org.zhouhe.vo.Article;
import org.zhouhe.vo.ArticleType;

public class ArticleTitleAndImg extends TagSupport {

	private static final long serialVersionUID = -5129179921556811260L;
	private ServletContext servletContext;
	private WebApplicationContext wac;
	private ArticleService articleService;

	private Integer typeId;
	private Integer resultSize = 10;
	private String className;

	public int doStartTag() {
		try {
			StringBuilder html = new StringBuilder();
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			servletContext = request.getSession().getServletContext();
			wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
			articleService = (ArticleService) wac.getBean("articleService");
			ArticleType articleType = articleService.getArticleType(typeId);
			List<Article> articleList = articleService.getArticleRecommended(typeId, resultSize);
			html.append("<div class='" + className + "'>");
			html.append("  <ul class='blockTag-title-ul'>");
			html.append("    <li class='blockTag-title-text'>" + articleType.getName() + "</li>");
			html.append("  </ul>");
			html.append("");
//			html.append("      <ul class='blockSimple-content-list-ul'>");
//			for(Article article : articleList){
//				html.append("    <li><img class='blockSimple-content-article-img' src='upload/images/article_img/"+ article.getImgName() +"' /></li>");
//				html.append("    <li><a href='" + article.getArticleId() + "'>" + article.getTitle() + "</a></li>");
//			}
//			html.append("      </ul>");
			html.append("      <div style='text-align:center'>");
			for(Article article : articleList){
				html.append("    <div class='blockSimple-content-img-div'><img src='"+ article.getImgName() +"' /></div>");
				html.append("    <div class='blockSimple-content-title-div'><span><a href='article.action?article.articleId=" + article.getArticleId() + "&articleType.articleTypeId="+ typeId +"' title='" + article.getTitle() + "'>" + article.getTitle() + "</a></span></div>");
			}
			html.append("      </div>");

			html.append("  <div style='clear:both;'></div>");
			html.append("</div>");
			html.append("");
			html.append("");
			html.append("");
			JspWriter out = pageContext.getOut();
			out.print(html.toString());
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}

	public Integer getTypeId() {
		return typeId;
	}

	public void setResultSize(Integer resultSize) {
		this.resultSize = resultSize;
	}

	public Integer getResultSize() {
		return resultSize;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getClassName() {
		return className;
	}

}
